#ifndef CS_REGISTRY_H
#define CS_REGISTRY_H

#include <boost\shared_ptr.hpp>
#include <boost\noncopyable.hpp>
#include <boost\bimap.hpp>

#include <map>
#include <list>
#include <string>

#include <interface\session_interface.h>
#include "message.h"
#include <utils\logger.h>
#include <utils\utils.h>

namespace cs
{
	class registry;
	typedef utils::singltone<registry> service_registry;

	class registry: public boost::noncopyable
	{
	public:
		typedef boost::shared_ptr<session_interface> session_ptr;
		typedef std::list<session_ptr> subscriber_list_t;

	public:
		registry();
		address add_session(session_ptr new_session);
		void remove_session(session_ptr session);
		address find_address(registry::session_ptr session);
		session_ptr find_session(const address& addr);
		bool register_service(session_ptr session, const std::string& service_name);
		address get_service_address(const std::string& service_name);
		address subscribe_sevice(session_ptr session, const std::string& service_name);
		subscriber_list_t get_subscribers(const std::string& service_name);

		void clear_address_generator();

	private:
		address last_addres_;
		typedef boost::bimap<address, session_ptr> sessions_map_t;
		typedef boost::bimap<address, std::string> services_map_t;
		typedef std::multimap<address, std::string> subscribers_map_t;

		sessions_map_t sessions_map;
		services_map_t services_map;
		subscribers_map_t subscribers_map;
		utils::logger logger;

		bool is_in_subscribers_map(const address& addr, const std::string& str);
	};
}
#endif